package com.snake.springboot.authority.service.impl;

import com.snake.springboot.authority.api.model.AuthOperatorRoleModel;
import com.snake.springboot.authority.api.request.AuthorizationQuery;
import com.snake.springboot.authority.api.response.AuthMenuRsp;
import com.snake.springboot.authority.api.response.AuthOperatorRoleRsp;
import com.snake.springboot.authority.api.service.AuthorizationService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthMenuTreeDO;
import com.snake.springboot.authority.dao.mybatis.entity.AuthOperatorRoleDO;
import com.snake.springboot.authority.dao.mybatis.entity.AuthOperatorRoleExtDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthOperatorRoleMapper;
import com.snake.springboot.authority.service.assembler.AuthMenuAssembler;
import org.snake.common.security.account.LoginAccount;
import org.snake.common.utils.CollectionUtils;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 类 {@code AuthorizationServiceImpl}  <br> .
 * <p>权限授权服务实现类
 * 创建时间：2024/5/28 16:54
 *
 * @author Yzhui
 * @see
 */
@Service
public class AuthorizationServiceImpl implements AuthorizationService {

    private AuthOperatorRoleMapper authOperatorRoleMapper;

    @Autowired
    public AuthorizationServiceImpl(AuthOperatorRoleMapper authOperatorRoleMapper) {
        this.authOperatorRoleMapper = authOperatorRoleMapper;
    }

    @Override
    public int authorization(List<AuthOperatorRoleModel> models) {
        List<AuthOperatorRoleDO> records = ListUtils.copy(models, AuthOperatorRoleDO.class);
        AuthOperatorRoleDO key = new AuthOperatorRoleDO(records.get(0).getOperatorId(), null);
        authOperatorRoleMapper.delete(key);
        return authOperatorRoleMapper.insertBatch(records);
    }


    @Override
    public int cancelAuthorization(String operatorId) {
        return authOperatorRoleMapper.delete(new AuthOperatorRoleDO(operatorId, null));
    }

    private  List<AuthOperatorRoleExtDO> queryAuthOperatorRoleExtList(String operatorId){
        List<AuthOperatorRoleExtDO> list = authOperatorRoleMapper.findRoleCodes(operatorId);
        return list;
    }

    @Override
    public Set<String> queryPerms(String operatorId) {
        List<AuthOperatorRoleExtDO> list = queryAuthOperatorRoleExtList(operatorId);
        return list.stream().map(AuthOperatorRoleExtDO::getRoleCode).collect(Collectors.toSet());
    }

    @Override
    public List<AuthOperatorRoleRsp> queryOperatorRole(String operatorId) {
       List<AuthOperatorRoleExtDO> list = queryAuthOperatorRoleExtList(operatorId);
       List<AuthOperatorRoleRsp> result = ListUtils.copy(list, AuthOperatorRoleRsp.class);
       return result;
    }

    @Override
    public List<AuthMenuRsp> queryAuthMenuTree(String operatorId) {
        List<AuthMenuTreeDO> list = authOperatorRoleMapper.findMenuTree(operatorId, null);
        List<AuthMenuRsp> result = ListUtils.copy(list, AuthMenuRsp.class);
        return result;
    }



    @Override
    public List<AuthMenuRsp> queryAuthMenuTree(AuthorizationQuery query) {
        Set<String> perms = LoginAccount.getAccount().getPerms();
        if (CollectionUtils.isNullOrEmpty(perms)) {
            return Collections.emptyList();
        }

        List<AuthMenuTreeDO> list = authOperatorRoleMapper.findMenuTreeByPerms(perms, query.getSystemId());
        List<AuthMenuRsp> result = AuthMenuAssembler.formatTree(list);
        return result;
    }
}
